package com.mju.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mju.controller.common.BaseController;
import com.mju.controller.common.JsonResult;
import com.mju.pojo.Favorites;
import com.mju.service.IFavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/favorites")
public class FavoritesController extends BaseController {
    @Autowired
    IFavoritesService favoritesService;

    @PostMapping("/addFavorite")
    public JsonResult<Integer> addFavorites(Integer pid, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String uid = parseTokenGetUid(token);
        //执行插入操作并返回fid
        Integer data = favoritesService.addFavorites(Integer.parseInt(uid), pid);
        return new JsonResult<>(OK,data);
    }

    @GetMapping("/queryFavorites")
    public JsonResult<IPage<Favorites>> queryFavorites(HttpServletRequest request, Integer currentPage,
                                                       Integer pageSize, Integer status) {
        String token = request.getHeader("Authorization");
        String uid = parseTokenGetUid(token);
        IPage<Favorites> pages = favoritesService.queryFavorites(Integer.parseInt(uid), currentPage, pageSize, status);
        return new JsonResult<>(OK, pages);
    }

    //处理取消收藏的请求
    @PostMapping("/updateStatus")
    public JsonResult<Void> cancelFavorites(HttpServletRequest request,Integer status,Integer fid){
        String token = request.getHeader("Authorization");
        String uid = parseTokenGetUid(token);
        favoritesService.updateFavoritesStatus(status,fid,Integer.parseInt(uid));
        return new JsonResult<>(OK);
    }

}
